package com.dao;

import com.pojo.SysUser;
import com.vo.UserVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @author mornd
 * @date 2020/11/12 - 14:02
 */
public interface UserDao {
    //根据用户名称查询用户角色集合
    @Select("SELECT * FROM sys_user WHERE userName = #{userName}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "userName", column = "userName"),
            @Result(property = "password", column = "password"),
            @Result(property = "realName", column = "realName"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "deptId", column = "deptId"),
            @Result(property = "status", column = "status"),
            @Result(property = "email", column = "email"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "userType", column = "userType"),
            @Result(property = "hireDate", column = "hireDate"),
            @Result(property = "createdBy", column = "createdBy"),
            @Result(property = "createDate", column = "createDate"),
            @Result(property = "modifyBy", column = "modifyBy"),
            @Result(property = "modifyDate", column = "modifyDate"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "roleList", column = "id", javaType = List.class, many = @Many(select = "com.dao.RoleDao.findRoleByUserId", fetchType = FetchType.LAZY))
    })
    SysUser findByName(String userName);

    //查询所有用户
    List<SysUser> findUserList(UserVo userVo);

    //根据deptId查询user个数
    @Select("select count(1) from sys_user where deptId = #{deptId}")
    int checkDeptByUser(Integer deptId);

    //添加用户
    int addUser(SysUser sysUser);

    //添加的用户默认角色为用户
    int addByDefaultRole(@Param("userId") Integer userId, @Param("roleId") Integer roleId);

    //修改用户
    int updateUser(SysUser sysUser);

    //删除用户信息
    int deleteUser(Integer userId);

    //给用户添加角色
    //查询用户在用户角色表中所占角色的个数 判断该部门是否可以删除
    int findUserByRoleCount(Integer userId);

    //删除用户在用户角色表中的信息
    int deleteDelUserByUserRole(Integer userId);

    //根据用户名称查询用户信息
    @Select("select * from sys_user where userName = #{userName}")
    SysUser findUserInfoByUserName(String userName);

    //给用户添加默认角色
    int addRoleByuserId(@Param("ids") List<Integer> ids, @Param("userId") Integer userId);

    @Select("select * from sys_user where id = #{id}")
        //根据用户id查询用户信息
    SysUser findUserById(Integer id);

}
